<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class adminController extends MY_Controller{
	public function __construct(){
		parent::__construct();
        
        $username = $this->session->userdata('username');
        if(!$username) redirect(base_url().'login');
	}
    
    public function create_unique_slug($string, $table)
    {
        $slug = url_title($string);
        $slug = strtolower($slug);
        $i = 0;
        $params = array ();
        $params['slug'] = $slug;
        if ($this->input->post('id')) {
            $params['id !='] = $this->input->post('id');
        }
        
        while ($this->db->where($params)->get($table)->num_rows()) {
            if (!preg_match ('/-{1}[0-9]+$/', $slug )) {
                $slug .= '-' . ++$i;
            } else {
                $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );
            }
            $params ['slug'] = $slug;
            }
        return $slug;
    } 
}